namespace AltNetUK.Models
{
    using System;
    using System.Collections.Generic;
    using SimpleWebServices.Data;
    using System.Globalization;

    public sealed class Registrations
    {
        public Registrations(string key)
        {
            this.Availability = Availability.Load(key);
            IList<Registration> attendees = Repository.Query<Registration>(
                string.Format(CultureInfo.InvariantCulture, "/registration[@complete=\"true\"][@{0}=\"true\"]", key)
                );
            this.Attendees = new SortedDictionary<string, Registration>();
            foreach (Registration attendee in attendees)
            {
                if (this.Attendees.ContainsKey(attendee.Name))
                {
                    this.Attendees.Add(attendee.Name + Guid.NewGuid().ToString(), attendee);
                }
                else
                {
                    this.Attendees.Add(attendee.Name, attendee);
                }
            }
        }

        public SortedDictionary<string, Registration> Attendees
        {
            get;
            private set;
        }

        public Availability Availability
        {
            get;
            private set;
        }

        public int Remaining
        {
            get
            {
                return this.Availability.Total - this.Attendees.Count;
            }
        }

        public bool IsFull
        {
            get
            {
                return 1 > this.Remaining;
            }
        }
    }
}